package com.vates.reservas.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vates.reservas.dominio.Cliente;
import com.vates.reservas.dominio.Hotel;
import com.vates.reservas.service.HotelService;

@Controller
@RequestMapping(value = "/hotel/alta")
public class HotelAltaController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private HotelService hotelService;

	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(@Valid Hotel hotel, BindingResult result) {
		logger.info("BindingResult: " + result);
		if (result.hasErrors()) {
			return "hotel";
		}
		String nombre = hotel.getNombre();
		String categoria = hotel.getCategoria();
		String domicilio = hotel.getDomicilio();
		String localidad = hotel.getLocalidad();
		String provincia = hotel.getProvincia();
		String codigoPostal = hotel.getCodigoPostal();
		String telefono = hotel.getTelefono();
		
		hotelService.alta(nombre, categoria, domicilio, localidad, provincia,
				codigoPostal, telefono);

		return "redirect:/hotel/listado";
	}

	@RequestMapping(method = RequestMethod.GET)
	protected Hotel formBackingObject(HttpServletRequest request)
			throws ServletException {
		return new Hotel();
	}

	public void setClienteService(HotelService hotelService) {
		this.hotelService = hotelService;
	}

}